/*******************************************************************************
 * Copyright (c) 2005, 2014 springside.github.io
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *******************************************************************************/
package com.chanjet.donkey.repository;

import java.util.List;

import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;

import com.chanjet.donkey.entity.Tree;

public interface TreeDao extends PagingAndSortingRepository<Tree, Long> {
	
//	@Query("from Tree where lft<=")
//	Tree findParent();
	
	@Query("from Tree where tree.id=? and lft>=? and rgt<=? order by lft asc")
	List<Tree> subtree(Long treeId, long lft, long rgt);
	
	@Query("from Tree where lft=1 order by id asc")
	List<Tree> findRoots();
	
	@Query("from Tree where rgt=lft+1 order by tree_id, lft")
	List<Tree> findLeaves();
	
	@Modifying
	@Query("update Tree set lft=lft+?3 where tree.id=?1 and lft>=?2")
	void shiftLft(Long treeId, long v, long delta);

	@Modifying
	@Query("update Tree set rgt=rgt+?3 where tree.id=?1 and rgt>=?2")
	void shiftRgt(Long treeId, long v, long delta);
}
